﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GoShopping.Web.ProductManager
{
    public partial class TypePropertyManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.ElementSelectedChange = GetTreeView;
                SetddlColumnType();
                SetddlSelectTypeValue();
                SetddlType();
                SetddlType2();
            }
        }
                                
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
        }
        private void SetddlSelectTypeValue()
        {

        }
        #region Set DropDownList Data
        private void SetddlType2()
        {
            SetControlCommon.GetDDLType(ddlType2);
            GetGVData(ddlType2);
        }

        private void SetddlType()
        {
            SetControlCommon.GetDDLType(ddlType);
            GetGVData(ddlType);
        }

        private void SetddlColumnType()
        {
            ddlColumnType.Items.Add(new ListItem("--请选择--"));
            foreach (string pv in Enum.GetNames(typeof(Model.Enumerations.PropertyValueType)))
            {
                ddlColumnType.Items.Add(pv);
            }
        }
        #endregion

        #region Control Event
        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetGVData((DropDownList)sender);

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Model.CustomDefine.ProductTypeProperty ptp=new GoShopping.Model.CustomDefine.ProductTypeProperty();

            if (string.IsNullOrEmpty(ddlType2.SelectedItem.Value))
            {
                //没有类型,无法添加
                return;
            }
            if (string.IsNullOrEmpty(zlTxtChName.Text.Trim()))
            {
                //类型名不能为空
                return;
            }
            ptp.ProdyctTypeID = Convert.ToInt16(ddlType2.SelectedItem.Value);
            ptp.PropertyName = zlTxtChName.Text.Trim();
            if (ddlColumnType.SelectedItem.Text == null)
            {
                //do something
                return;
            }
            ptp.ValueType = (Model.Enumerations.PropertyValueType)Enum.Parse(typeof(Model.Enumerations.PropertyValueType), ddlColumnType.SelectedItem.Text);
            if (txtSelectTypeValue.Text != "")
            {
                ptp.SelectTypeValue = txtSelectTypeValue.Text;
            }
            else
            {
                ptp.SelectTypeValue = "";
            }
            
            BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
            try 
            { 
                ptb.AddProductTypeProperty(ptp);
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('添加成功!')</script>", false);
                GetGVData(ddlType2);
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1,typeof(UpdatePanel), null, "<script>alert('添加失败!')</script>", false);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            zlTxtChName.Text = "";
            ddlColumnType.SelectedIndex = -1;
            txtSelectTypeValue.Text = "";
            ddlType2.SelectedIndex = -1;
        }

        protected void extGVTP_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "TPEdit")
            {
                hidField.Value = ddlType.SelectedItem.Value;
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            // 使用ArrayList记录选择的编号
            ArrayList selList = new ArrayList();
            Dictionary<int, string> dicTypeProperty = new Dictionary<int, string>();
            int i = 0;
            // 遍历数据集
            foreach (GridViewRow row in this.extGVTP.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                //// 若为Checked，将信息添加到ArrayList
                dicTypeProperty.Add(Convert.ToInt16(ddlType.SelectedItem.Value), ((Label)row.FindControl("lblTypeProColumn")).Text);
                //selList.Add(((Label)row.FindControl("lblID")).Text);
                i++;
            }
            // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
            if (selList.Count == 0)
            {
                return;
            }
            // 调用逻辑方法，执行删除操作
            BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
            foreach (KeyValuePair<int, string> kvp in dicTypeProperty)
            {
                ptb.DelProductTypeProperty(kvp.Key, kvp.Value);
            }
            // 重新绑定列表
            this.extGVTP.DataSource = GetGVData(ddlType);
            this.extGVTP.DataBind();
        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.extGVTP.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void extGVTP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVTP.PageIndex = e.NewPageIndex;
            GetGVData(ddlType);
        }
        #endregion

        #region common method
        private IList<Model.CustomDefine.ProductTypeProperty> GetGVData(DropDownList ddl)
        {
            if (ddl.SelectedIndex == -1)
            {
                return null;
            }
            BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
            IList<Model.CustomDefine.ProductTypeProperty> iTPList = ptb.GetProductTypeProperties(Convert.ToInt16(ddl.SelectedItem.Value));
            this.extGVTP.DataSource = iTPList;
            this.extGVTP.DataBind();
            return iTPList;
        }
        #endregion
    }
}
